package com.shujia.day13;

import java.util.Random;

public class Demo06EatWatermelon {

    public static void main(String[] args) {

        PeopleThread thread1 = new PeopleThread("许康杰");
        PeopleThread thread2 = new PeopleThread("李亮");
        thread1.start();
        thread2.start();

    }

    static class PeopleThread extends Thread {
        int eatNum = 0;  // 每个线程都可以维护自身的变量
        Random random;

        public PeopleThread(String name) {
            super(name);
            random = new Random();
        }

        @Override
        public void run() {
            while (true) {
                int i = random.nextInt(3);
                try {
                    Thread.sleep(i * 1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                eatNum += 1;
                System.out.println("当前线程:" + this.getName() + "正在吃第" + eatNum + "块西瓜");
                if (eatNum % 13 == 0){
                    System.out.println("当前线程:" + this.getName() + "吃到一颗坏瓜... 吐了...");
                }
            }
        }
    }
}

